﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GcmpModelInterface.h"
#include "UniIdentity.h"
#include "IDbObject.h"

namespace gcmp
{
    class IDocument;

    /// \brief uid到子节点uid集合映射的数组
    typedef std::vector<std::pair<UniIdentity, std::vector<UniIdentity>>> UidToChildrenUidsVector;

    /// \brief Uid父子层次关系
    ///
    /// 记录Uid的父子层次关系
    class GCMP_MODEL_INTERFACE_EXPORT IUidTree : public IDbObject
    {
        DEFINE_CAST_DERIVED(gcmp::IUidTree, gcmp::IDbObject)
    public:
        /// \brief 给父节点添加子节点的快捷方法
        ///
        /// \param pRoot Uid树的根节点
        /// \param parentUid 待插入子节点的父节点Uid
        /// \param childUid 子节点Uid
        static void AddChildUid(IUidTree* pRoot, 
            const UniIdentity& parentUid, const UniIdentity& childUid);
    public:
        /// \brief 析构函数
        virtual ~IUidTree() {}

        /// \brief 获取uid到子节点uid集合映射的数组
        /// \return uid到子节点uid集合映射的数组
        virtual UidToChildrenUidsVector GetUidToChildrenUidsVector() const = 0;

        /// \brief 获取系统内置的根节点
        ///
        /// 系统提供内置的虚拟根节点，未指定根节点的节点会被挂在虚拟根节点下
        /// \return 系统内置根节点的uid
        virtual const UniIdentity& GetRootUid() const = 0;

        /// \brief 重置Uid父子层次关系
        /// \param uidToChildrenUidsMap 新的Uid父子层次关系中，uid到子节点uid集合的映射
        /// \return true-重置成功；false-重置失败（如：树结构不合法）
        virtual bool ResetUidTree(const UidToChildrenUidsVector& uidToChildrenUidsMap) = 0;

        /// \brief 添加新的节点
        /// \param uidToChildrenUidsMap 要添加节点的uid到其子节点uid集合的映射
        /// \return true-添加成功；false-添加失败（如：树结构不合法）
        virtual bool AddUidTreeNodes(const UidToChildrenUidsVector& uidToChildrenUidsMap) = 0;

        /// \brief 获取目标节点的直接子节点
        /// \param uid 目标节点的uid
        /// \return 目标节点直接子节点的uid集合
        virtual const std::vector<UniIdentity>& GetChildrenUids(const UniIdentity & uid) const = 0;

        /// \brief 获取目标节点所有后代的uid集合
        ///
        /// 此接口返回目标节点的所有子孙后代节点
        /// \param uid 目标节点的uid
        /// \return 目标节点所有后代的uid集合
        virtual std::vector<UniIdentity> GetDescendantUids(const UniIdentity & uid) const = 0;

        /// \brief 获取目标节点父节点的uid
        /// \param uid 目标节点的uid
        /// \return 目标节点父节点的uid
        virtual const UniIdentity& GetParentUid(const UniIdentity & uid) const = 0;

        /// \brief 设置目标节点父节点的uid
        /// \param uid 目标节点的uid
        /// \param parentUid 目标节点父节点的uid
        /// \return true-设置成功；false-设置失败（如：不存在此目标节点；不存在此父节点；树结构不合法）
        virtual bool SetParentUid(const UniIdentity & uid, const UniIdentity & parentUid) = 0;

        /// \brief 给目标节点添加子节点
        /// \param uid 目标节点的uid
        /// \param childUid 新添加的子节点的uid
        /// \return true-添加成功；false-添加失败（如：原先已包含此子节点；目标节点不存在；树结构不合法）
        virtual bool AddChildUid(const UniIdentity& uid, const UniIdentity& childUid) = 0;

        /// \brief 删除目标节点
        /// 
        /// 刪除一个节点，其子节点会自动被刪除，其父节点会自动更新子节点列表
        /// \param uid 目标节点的uid
        /// \return true-删除成功；false-删除失败（如：不存在此目标节点）
        virtual bool DeleteTreeNode(const UniIdentity& uid) = 0;

        /// \brief 判断当前UidTree是否存在指定节点uid
        ///
        /// \param uid  待查找Uid
        /// \return bool true-存在, false-不存在
        virtual bool HasUid(const UniIdentity& uid) const = 0;
    };
}


